#include "Wave.h"
#include "WaveManager.h"
#include "GameUtil.h"

using namespace cocos2d;

CCArray* Wave::getWave(int waveNum)
{
	CCArray* waveData = new CCArray();

	switch(waveNum) {
	case 1:
		createWave1(waveData);
		break;
	default:
		createWaveRandom(waveData, waveNum);
		break;
	}

	return waveData;

}

void Wave::releaseWave(CCArray* data)
{
	//TODO
	CCObject *obj = NULL;  
	CCObject *enemy = NULL;  

	CCARRAY_FOREACH(data, obj)
	{
		GroupData* groupData = (GroupData*)obj;
		CCARRAY_FOREACH(groupData->d_enemyData, enemy)
		{
			OneEnemy* enemyData = (OneEnemy*)enemy;
			enemyData->release();
		}
		groupData->release();
	}
	data->release();
}

void Wave::createWave1(CCArray* data)
{
	GroupData* group;
	CCArray* enemyData;
	OneEnemy* one;

	//--------------------------------------
	createGroup5Arrow(data, ENEMY_TYPE_1, .5f);

	//--------------------------------------
	createGroupRandom(data);

	//--------------------------------------
	group = new GroupData();
	data->addObject(group);
	enemyData = new CCArray();
	group->d_enemyData = enemyData;
	group->d_delay = 9.0f;

	one = new OneEnemy();
	one->d_type = ENEMY_TYPE_2;
	one->d_pos = ccp(1, 2);
	enemyData->addObject(one);

	one = new OneEnemy();
	one->d_type = ENEMY_TYPE_2;
	one->d_pos = ccp(1, 4);
	enemyData->addObject(one);

	//--------------------------------------
	createGroupRandom(data);

	//--------------------------------------
	createGroup5Arrow(data, ENEMY_TYPE_3, 9.0f);
	
	//--------------------------------------
	createGroupRandom(data);

	//--------------------------------------
	group = new GroupData();
	data->addObject(group);
	enemyData = new CCArray();
	group->d_enemyData = enemyData;
	group->d_delay = 9.0f;

	createGroupLine(enemyData, ENEMY_TYPE_1, 2, 4);
	createGroupLine(enemyData, ENEMY_TYPE_1, 4, 4);

	//--------------------------------------
	createGroupRandom(data);
}

void Wave::createWaveRandom(CCArray* data, int wave)
{
	int groupNum = 10 + wave/10;
	while(groupNum > 0) {
		groupNum--;
		createGroupRandom(data);
	}
}

void Wave::createGroup5Arrow(CCArray* data, EEnemyType type, float delay)
{
	GroupData* group;
	CCArray* enemyData;
	OneEnemy* one;

	group = new GroupData();
	data->addObject(group);
	enemyData = new CCArray();
	group->d_enemyData = enemyData;
	group->d_delay = delay;

	one = new OneEnemy();
	one->d_type = type;
	one->d_pos = ccp(2, 1);
	enemyData->addObject(one);

	one = new OneEnemy();
	one->d_type = type;
	one->d_pos = ccp(1.5, 2);
	enemyData->addObject(one);

	one = new OneEnemy();
	one->d_type = type;
	one->d_pos = ccp(1, 3);
	enemyData->addObject(one);

	one = new OneEnemy();
	one->d_type = type;
	one->d_pos = ccp(1.5, 4);
	enemyData->addObject(one);

	one = new OneEnemy();
	one->d_type = type;
	one->d_pos = ccp(2, 5);
	enemyData->addObject(one);
}

void Wave::createGroupLine(CCArray* enemyData, EEnemyType type, int lineNum, int count)
{
	OneEnemy* one;

	for(int i = 1; i <= count; i++) {
		one = new OneEnemy();
		one->d_type = type;
		one->d_pos = ccp(i, lineNum);
		enemyData->addObject(one);
	}
}

void Wave::createGroupRandom(CCArray* data)
{
	GroupData* group;
	CCArray* enemyData;
	OneEnemy* one;

	group = new GroupData();
	data->addObject(group);
	enemyData = new CCArray();
	group->d_enemyData = enemyData;
	group->d_delay = 5.0f;

	int num = GameUtil::random(1, 3);
	while(num > 0) {
		num--;
		one = new OneEnemy();
		one->d_type = (EEnemyType)GameUtil::random(NUM_OF_ENEMY_TYPE);
		one->d_pos = ccp(GameUtil::random(1, 3), GameUtil::random(1, 5));
		enemyData->addObject(one);
	}
}

